package com.wyzz.hr.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;

public class SystemPropertiesReader {

	private SystemPropertiesReader() {
		InputStream in;
		properties = new Properties();
		in = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("leaverequest_zh_CN.properties");
		try {
			properties.load(in);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		if (in != null)
			try {
				in.close();
			}
			// Misplaced declaration of an exception variable
			catch (IOException e) {
			}
	}

	public static void reInit() {
		properties.clear();
		PropertiesConfiguration config = null;
		try {
			config = new PropertiesConfiguration(
					"leaverequest_zh_CN.properties");
			config.setEncoding("utf-8");
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		String key;
		for (Iterator it = config.getKeys(); it.hasNext(); properties.put(key,
				(String) config.getProperty(key)))
			key = (String) it.next();

	}

	public static SystemPropertiesReader getInstance() {
		return reader;
	}

	public String getValue(String key) {
		return properties.getProperty(key);
	}

	public String getValue(String key, String defaultValue) {
		String rs = getValue(key);
		return StringUtils.defaultIfEmpty(rs, defaultValue);
	}

	public boolean getBoolean(String key) {
		String value = getValue(key);
		return "true".equalsIgnoreCase(value);
	}

	private static final String FILE_NAME = "leaverequest_zh_CN.properties";
	private static SystemPropertiesReader reader = new SystemPropertiesReader();
	private static Properties properties;

}
